﻿using System;

namespace RexToy.UI.Controls
{
    public abstract class ChoiceGroup : Container
    {
        protected bool _multi = true;

        public string[] events = new string[] { "onchange" };

        public ChoiceGroup(bool allowMulti)
        {
            _multi = allowMulti;
        }

        public override void AppendControl(Control ctrl)
        {
            ctrl.Standalone = false;
            if (this.__controls.Length == 0)
            {
                ctrl.SetFirst(true);
            }

            var lastCtrl = this.__controls.Last();
            if (lastCtrl != null)
            {
                lastCtrl.SetLast(false);
            }

            this.Super(ctrl);
            ctrl.SetLast(true);
        }

        public override void RemoveControl(Control ctrl)
        {
            this.Super(ctrl);

            var firstCtrl = this.__controls.First();
            if (firstCtrl != null)
            {
                firstCtrl.SetFirst(true);
            }

            var lastCtrl = this.__controls.Last();
            if (lastCtrl != null)
            {
                lastCtrl.SetLast(true);
            }
        }

        public abstract void AppendChoice(string text, object value);

        public abstract void RemoveChoice(object value);

        public abstract void ClearChoice();

        public Control GetControl(uint index)
        {
            return this.__controls[index];
        }

        public int GetSelectedIndex()
        {
            if (this._multi)
                throw new Exception("MultiChoiceGroup, use getSelectedIndexes()");
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (ch.Checked)
                {
                    return i;
                }
            }
            return -1;
        }

        public int[] GetSelectedIndexes()
        {
            if (!this._multi)
                throw new Exception("MultiChoiceGroup, use getSelectedIndex()");
            var ret = new int[0];
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (ch.Checked)
                {
                    ret.Push(i);
                }
            }
            return ret;
        }

        public object GetSelectedValue()
        {
            if (this._multi)
                throw new Exception("MultiChoiceGroup, use getSelectedValues()");
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (ch.Checked)
                    return ch.Value;
            }
            return null;
        }

        public object[] GetSelectedValues()
        {
            if (!this._multi)
                throw new Exception("MultiChoiceGroup, use getSelectedValue()");
            var ret = new object[0];
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (ch.Checked)
                    ret.Push(ch.Value);
            }
            return ret;
        }

        public void SetSelectedValue(object v)
        {
            if (this._multi)
                throw new Exception("MultiChoiceGroup, use setSelectedValues()");
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (v == ch.Value)
                    ch.Checked = true;
                else
                    ch.Checked = false;
            }
        }

        public void SetSelectedValues(object[] arr)
        {
            if (!this._multi)
                throw new Exception("MultiChoiceGroup, use setSelectedValue()");
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                if (arr.Contains(ch.Value))
                    ch.Checked = true;
                else
                    ch.Checked = false;
            }
        }
        public void SetEnable(bool enable)
        {
            for (var i = 0; i < this.__controls.Length; i++)
            {
                var ch = this.__controls[i] as IChoiceControl;
                ch.Enable = enable;
            }
        }
    }
}
